$(function () {
    //科室的动态生成
    $.ajax({
        // ansyc: false,
        url: "/hospital/patient/register/office/get",
        type: "GET",
        // contentType:"application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
                $("#office-select").html("");
                $("#office-select").append("<option value=''>请选择科室</option>");
                $.each(data, function (index, ele) {
                    $("#office-select").append("<option value='" + ele.officeId + "'>" + ele.officeName + "</option>");
                });
        },
        error: function () {
            alert("数据加载失败！");
        }
    });
    //保存按钮监听事件
    $("#register-submit").click(function () {
        var name = $('#set-name').val();
        var age = $('#set-age').val();
        var nation = $('#set-nation').val();
        var contact = $('#set-contact').val();
        var depart = $('#office-select option:selected').val();
        var sex = $('#sex-select option:selected').val();
        var remark = $('#set-remark').val();//用数据库中的remark字段保存身份证号
        //此ajax用来插入新的病人
        $.ajax({
            url: "/hospital/patient/register/insert",
            type: "post",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({
                "patName": name,
                "patAge": age,
                "patNation": nation,
                "patContact": contact,
                "patOfcId": depart,
                "patSex": sex,
                "patRemark": remark
                // "patCreateTime":date
            }),
            success: function (data) {
                //此ajax用来显示刚插入的新的病人的信息
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer.msg('挂号成功，请耐心等待', {
                        time: 1500, //1.5s后自动关闭
                    });
                });
                $.ajax({
                    url:"/hospital/patient/register/select/newpatient",
                    type:"GET",
                    dataType:"json",
                    success:function (data) {
                        $("#tbody").html("");
                        $.each(data,function (index,ele) {
                            $("#tbody").append("<tr>\n" +
                                "                <td>"+ele.patConven+"</td>\n" +
                                "                <td>"+ele.patName+"</td>\n" +
                                "                <td>"+ele.patSex+"</td>\n" +
                                "                <td>"+ele.patAge+"</td>\n" +
                                "                <td>"+ele.patRemark+"</td>\n" +
                                "                <td>"+ele.patContact+"</td>\n" +
                                "                <td>"+ele.patOfcName+"</td>\n" +
                                "                <td>"+ele.patCreateTime+"</td>\n" +
                                "                <td>"+ele.patStatus+"</td>\n" +
                                "            </tr>")
                        });
                    },
                    error:function () {
                     alert("数据加载失败!!!");
                    }
                });
            }
        });
    });

    //重置按钮监听事件
    $("#register-clear").click(function () {
        $('#set-contact').val("");
        $('#set-age').val("");
        $('#set-name').val("");
        $('#set-nation').val("");
        $('#office-select').val("");
        $('#sex-select').val();
        $('#set-remark').val("");
    });
    //返回主页面监听事件
    $("#register-exit").click(function () {
        $(".register-info").load("dia-console.html");
    });
    //打印挂号信息按钮监听
    $("#register-print").click(function () {
       $("#printInfo").css("display","block");
       $(".register-info").css("display","none");
       //信息填充
        $.ajax({
            url:"/hospital/patient/register/select/newpatient",
            type:"GET",
            dataType:"json",
            success:function (data) {
                $.each(data,function (index,ele) {
                 $("#show-num").val(ele.patConven);
                 $("#show-name").val(ele.patName);
                 $("#show-sex").val(ele.patSex);
                 $("#show-age").val(ele.patAge);
                 $("#show-id").val(ele.patRemark);
                 $("#show-office").val(ele.patOfcName);
                 $("#show-contact").val(ele.patContact);
                });
            },
            error:function () {
                alert("数据加载失败!!!");
            }
        });
    });
    //确定打印按钮监听事件
    $("#register-sure-print").click(function () {
        window.print();
    });
    //确定打印时返回按钮监听事件
    $("#register-sure-back").click(function () {
        $("#printInfo").css("display","none");
        $(".register-info").css("display","block");
    });
});